# -*- coding: UTF-8 -*-
import re
import requests
import ssl
import urllib2
import urllib
import time
from PIL import Image
import json
import os
import cookielib
from lxml import etree
import sys


print("hello python")

header = {
#'Host':"www.zhihu.com",
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
'Accept':"*/*",
'Accept-Language':"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
'Accept-Encoding':"gzip, deflate",
'Content-Type':"application/x-www-form-urlencoded; charset=UTF-8"
}

session=requests.session()



def getContent(url):
    # session=requests.session()
    r=session.get(url,headers=header,verify=False)
    # print(r.content)
    return r.content


def getXSRF(url):
    
    content=getContent(url)

    tree=etree.HTML(content)

    nodes=tree.xpath("//input[@name='_xsrf']/[last()]")
    print(nodes)

    # print(node.get('value'))

    # pattern=re.compile('.*?<input type="hidden" name="_xsrf" value="(.*?)"/>.*?')
    # match=re.findall(pattern,content)
    # xsrf=match[0]
    # return xsrf

def getXSRF1(url):
    context = ssl._create_unverified_context()
    r=urllib2.urlopen(url, context=context)
    content=r.read().decode("UTF-8")
    pattern=re.compile('.*?<input type="hidden" name="_xsrf" value="(.*?)"/>.*?')
    match=re.findall(pattern,content)
    xsrf=match[0]    
    return xsrf

def get_captcha():
    """
    """
    t = str(int(time.time() * 1000))

    captcha_url = 'https://www.zhihu.com/captcha.gif?r=' + t + "&type=login"
    urllib.urlretrieve(captcha_url, 'cptcha.gif')
    # image_data = request.urlopen(captcha_url).read()
    # with open('cptcha.gif', 'wb') as f:
    #     f.write(image_data)
    im = Image.open('cptcha.gif')
    im.show()
    captcha = raw_input('input captcha:')
    return captcha


def login(base_url,username=None,password=None):
    login_data = {
       "_xsrf":getXSRF1(base_url),
       "remenberme":"true",
       "password":"9459wtang",
       "phone_num":"15682051720",
       "captcha":get_captcha()     
    }
    

    session=requests.session()
    base_url+="login/phone_num"
    response=session.post(base_url,headers=header,data=login_data)


    f=open("res.txt","w+")

    f.write(response.content.decode("UTF-8"))

    print(response.content.decode("UTF-8"))    



url="https://www.zhihu.com/"

#login(url)

# im=Image.open('cptcha.gif')
# im.show()

# s=getContent(url)

# tree=etree.HTML(r.read().decode())

# nodes=tree.xpath(u"//input[@name='_xsrf']")

# print(nodes)

#c=getContent(url)
#print(c)


myPage = '''<html>
        <title>TITLE</title>
        <body>
        <h1>我的博客</h1>
        <div>我的文章</div>
        <div id="photos">
         <img src="pic1.jpeg"/>
         <span id="pic1">PIC1 is beautiful!</span>
         <img src="pic2.jpeg"/>
         <span id="pic2">PIC2 is beautiful!</span>
         <p><a href="http://www.example.com/more_pic.html">更多美图</a></p>
         <a href="http://www.baidu.com">去往百度</a>
         <a href="http://www.163.com">去往网易</a>
         <a href="http://www.sohu.com">去往搜狐</a>
        </div>
        <p class="myclassname">Hello,\nworld!<br/>-- by Adam</p>
        <div class="foot">放在尾部的其他一些说明</div>
        </body>
        </html>'''


if __name__=="__main__":
    # s=zhihu_login().getXSRF()
    #ssl._create_default_https_context = ssl._create_unverified_context

    reload(sys)
    print(sys.getdefaultencoding())
    sys.setdefaultencoding("utf-8")

    str='唐伟'
    print(repr(str.decode('GBK')))



    # html = etree.fromstring(myPage)

    # text4 = html.xpath('//div[@class="foot"]/text()')

    # print(u'\u653e\u5728\u5c3e\u90e8\u7684\u5176\u4ed6\u4e00\u4e9b\u8bf4\u660e')
    # print(text4[0])

    # value1 = html.xpath('//a/@href')

    # print(value1)

    # value2 = html.xpath('//img/@src')
    # print(value2)

    # value3 = html.xpath('//div[2]/span/@id')
    # print(value3)

    # a_href = html.xpath('//div[2]//a/@href')
    # print(a_href)

    # a_href = html.xpath('//div[position()<3]/a/@href')
    # print(a_href)


    # print(session.headers)




    # cookie=requests.utils.dict_from_cookiejar(session.cookies)
    # print(cookie)




    # zhihu_login().login()    